<?php

namespace app\controller;
use app\BaseController;
use app\model\AppFun;
use app\model\SysAccount\SysRoleFun;
use app\model\SysRole;
use app\utils\CamlHump;
use app\utils\Lodash;
use app\utils\ResposeUtils;
use think\facade\Log;
use think\facade\Request;
use Underscore\Types\Arrays;

class SysRoleController  extends BaseController
{
    public function list()
    {
        $body = Request::post();
        $query = SysRole::createQuery($body);
        $list= CamlHump::toCamlArray($query->select());


        return ResposeUtils::ok(
            $list ,
            200,
            $query->count()
        );
    }
    function getbyid()
    {
        $id = Request::get("id");
        $one = SysRole::findOne($id);
        return ResposeUtils::ok(
            $one
        );
    }
    function update()
    {
        $body = Request::post();
        $updateersuilt=SysRole::updatePlus($body,$body['id']);
        return ResposeUtils::ok(
            $updateersuilt['id']
        );
    }
    function add()
    {
        $body = Request::post();
        $result=SysRole::addPlus($body);
        return ResposeUtils::ok(
            $result['id']
        );
    }
    function delbyid()
    {
        $id = Request::get('id');
        $result=SysRole::destroy($id);
        return ResposeUtils::ok(
            $result
        );
    }

    /*
     * 递归  不断获取新的数据
     * 获取 所有的菜单 tree*/
    function getAllTree(){
        $trees = AppFun::findListByParentById('0');
        for ($i = 0; $i < count($trees); $i++) {
            $trees[$i]=AppFun:: fillChild($trees[$i]);
        }

        return ResposeUtils::ok(
            $trees
        );

    }

    /*bing role fun [funId,roleId]*/
    function bindRoleFun(){
        $datalist = Request::post();


        /*
         * 1确定唯一 funId,roleId 幂等性  没有新增
           2找到 需要删除的
         */
        $roleId = $datalist[0]['roleId'] ;
        /*找到数据库现在有的*/
        $findExitInDatabaseList = CamlHump::toCamlArray(SysRoleFun::createQuery([
            "roleId" => $roleId,
        ])->select());

        $shouldDeleteRows=[];
        for ($i = 0; $i < count($findExitInDatabaseList); $i++) {
            $isexit=Arrays::find($datalist, function ($paramItem) use ($i, $findExitInDatabaseList) {
                return $paramItem['funId'] == $findExitInDatabaseList[$i]["funId"];
            });
            if(!$isexit){
                array_push($shouldDeleteRows, $findExitInDatabaseList[$i]);
            }

        }
        Arrays::each($shouldDeleteRows, function ($deleteRow){
            SysRoleFun::deletePlus($deleteRow['id']);
        });


        Arrays::each($datalist, function ($row) {
            //幂等性 是否存在
            if(!SysRoleFun::exitOne(SysRoleFun::createQuery([
                "funId" => $row['funId'],
                "roleId" => $row['roleId']
            ]))){
                //不存在新增
                SysRoleFun::addPlus([
                    "funId" => $row['funId'],
                    "roleId" => $row['roleId']
                ]);
            };

        });



        return ResposeUtils::ok(["ok"]);

    }

    /*获取 role_fun by roleId*/
    function getRoleAppFunByRoleId(){
        $roleId = Request::post('roleId');
       $menuSelect= CamlHump::toCamlArray(SysRoleFun::createQuery(["roleId" => $roleId]
       )->select());
        return ResposeUtils::ok($menuSelect);
    }
}